package com.taobao.machinesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.entity.McPlan0;
import com.taobao.machinesystem.entity.McPlan1;
import com.taobao.machinesystem.entity.SysUser;
import com.taobao.machinesystem.entity.vo.PatrolYear;
import com.taobao.machinesystem.mapper.McPlan0Mapper;
import com.taobao.machinesystem.mapper.McPlan1Mapper;
import com.taobao.machinesystem.service.McPlan0Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 巡查计划表(主表) 服务实现类
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@Service
public class McPlan0ServiceImpl extends ServiceImpl<McPlan0Mapper, McPlan0> implements McPlan0Service {

    @Autowired
    private McPlan0Mapper mcPlan0Mapper;

    @Autowired
    private McPlan1Mapper mcPlan1Mapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean createMcPlan(String year, List<McPlan1> mcPlan1s) {
        McPlan0 mcPlan0 = new McPlan0();
        mcPlan0.setYear(year);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        mcPlan0.setCreatedBy(sysUser.getUsername());
        mcPlan0.setFChecked("0");
        mcPlan0.setCheckedLead("");
        mcPlan0.setCheckedName("");
        //insertCount主单记录
        int mcPlan0Count = mcPlan0Mapper.insert(mcPlan0);

        log.info(mcPlan0.toString());

        for (McPlan1 mcPlan1 : mcPlan1s) {
            mcPlan1.setInspectId(mcPlan0.getId());
            //mcPlan1Count 从表记录
            int mcPlan1Count = mcPlan1Mapper.insert(mcPlan1);

            log.info(mcPlan1.toString());
            //判断
            if (mcPlan1Count <= 0) {
                log.error("{} 记录插入失败", mcPlan1.toString());
                throw new RuntimeException("记录插入失败");
            }
        }

        if (mcPlan0Count == 0) {
            return false;
        }

        return true;
    }

    @Override
    public Page<McPlan0> listMcPlan0(Integer currentPage, Integer size, String year) {
        Page<McPlan0> mcPlan0Page = new Page<McPlan0>(currentPage, size);

        QueryWrapper<McPlan0> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(year)) {
            queryWrapper.eq("year", year);
        }

        return baseMapper.selectPage(mcPlan0Page, queryWrapper);
    }

    @Override
    public boolean updateMcPlan(McPlan0 mcPlan0) {
        //审核计划单
        if ("1".equals(mcPlan0.getFChecked())) {
            log.error("{} 计划单已审核，不能在更改", mcPlan0.toString());
            throw new RuntimeException("计划单已审核，不能在更改");
        }

        //insertCount主单记录
        mcPlan0Mapper.updateById(mcPlan0);

        return true;
    }

    @Override
    public boolean deleteMcPlan0ById(String id) {
        McPlan0 mcPlan0 = baseMapper.selectById(id);

        if ("1".equals(mcPlan0.getFChecked())) {
            log.error("{} 计划单已审核，不允许删除", id);
            throw new RuntimeException("计划单已审核，不允许删除");
        }

        int i = baseMapper.deleteById(id);

        return i > 0 ? true : false;
    }

    @Override
    public boolean checkPlan(String planId) {
        McPlan0 mcPlan0 = baseMapper.selectById(planId);

        if ("1".equals(mcPlan0.getFChecked())){
            log.error("此巡查单已被"+mcPlan0.getCheckedName()+" 审核");
            throw new RuntimeException("此巡查单已被"+mcPlan0.getCheckedName()+" 审核");
        }
        Subject subject = SecurityUtils.getSubject();
        SysUser sysUser = (SysUser) subject.getPrincipal();
        mcPlan0.setYear(mcPlan0.getYear().substring(0,4));
        //todo shiro 获取信息
        mcPlan0.setFChecked("1");
        mcPlan0.setCheckedLead(sysUser.getId());
        mcPlan0.setCheckedName(sysUser.getUsername());

        int countUpdate = baseMapper.updateById(mcPlan0);

        return countUpdate>0?true:false;
    }

    @Override
    public List<PatrolYear> patrolByYear(String year) {
        if (StringUtils.isEmpty(year)){
            return null;
        }

        return baseMapper.patrolByYear(year);

    }
}
